package com.newlink.building.userinfo.activity

import android.text.SpannableStringBuilder
import android.view.View
import android.widget.TextView
import com.newlink.building.common_base.base.NL_BaseMvpActivity
import com.newlink.building.common_base.model.bean.AuthKeyTypePassword
import com.newlink.building.common_base.model.bean.AuthResponseBody
import com.newlink.building.common_base.model.bean.Base_BaseBean
import com.newlink.building.common_base.utils.NLog
import com.newlink.building.common_base.utils.ToastUtils
import com.newlink.building.userinfo.R
import com.newlink.building.userinfo.databinding.ActivityAuthPasswordBinding
import com.newlink.building.userinfo.mvp.Module_UserInfo_AuthPasswordPresenter
import com.newlink.building.userinfo.mvp.contract.Module_UserInfo_AuthPasswordContract

/**
 * @Author: Jake
 * @Date: 2023-12-14
 * @Description:
 */
class Module_UserInfo_AuthPasswordActivityNL
    : NL_BaseMvpActivity<Module_UserInfo_AuthPasswordContract.View, Module_UserInfo_AuthPasswordContract.Presenter>(),
    Module_UserInfo_AuthPasswordContract.View {

    override fun createPresenter(): Module_UserInfo_AuthPasswordContract.Presenter = Module_UserInfo_AuthPasswordPresenter()

    private var mLastPwd: String = ""

//    override fun attachLayoutRes(): Int = R.layout.activity_auth_password

    private lateinit var mBinding: ActivityAuthPasswordBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityAuthPasswordBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initData() {
        loadCurrentPassword()
    }

    private fun loadCurrentPassword() {

        showLoading()

        mPresenter?.loadCurrentPassword(accessToken, "$userId")
    }

    override fun initView() {
        super.initView()
        findViewById<TextView>(R.id.tv_title_bar_center).text = getString(R.string.auth_password)
        mBinding.btnAuthPasswordSubmit.setOnClickListener {


            val newPwd = mBinding.etAuthPassword.text.toString()

            if (newPwd.isNotBlank()) {

                if (newPwd == mLastPwd) {
                    ToastUtils.showTextToast(this, getString(R.string.auth_password_already_exist))
                    return@setOnClickListener
                }

                if (newPwd.length in 3..16) {
                    mPresenter?.updateOpenDoorPassword(
                        accessToken,
                        "$userId",
                        newPwd
                    )
                } else {
                    ToastUtils.showTextToast(this, getString(R.string.auth_password_length_tips))
                }

            }
        }
    }

    override fun onAuthConfirmResponse(result: Base_BaseBean?) {
        val res = if (result?.code == 200) {
            getString(R.string.auth_pwd_change_success)
        } else {
            result?.msg ?: getString(R.string.auth_pwd_change_failed)
        }
        ToastUtils.showTextToast(this, res)

        if (result?.code == 200) {
            finish()
        }
    }

    override fun getPasswordSuccess(pwdResult: AuthResponseBody?) {
        NLog.e("<jake> ... pwdResult is $pwdResult")
        hideLoading()
        if (pwdResult != null) {
            if (pwdResult.keyType != AuthKeyTypePassword) {
                return
            }
            pwdResult.keyInfo.let {
                mLastPwd = it
                mBinding.etAuthPassword.text = SpannableStringBuilder(it)
            }
        }
    }

    override fun getPasswordFailed(msg: String) {
        hideLoading()
    }
}